package com.gzhryc.shared_device.oem.code.user.services;

import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.base.models.OrderLogContent;
import com.gzhryc.shared_device.oem.code.user.dao.UserTransferMoneyOrderLogDao;
import com.gzhryc.shared_device.oem.code.user.dao.db.UserTransferMoneyOrderLog;

import java.sql.SQLException;
import java.util.Date;

public class UserTransferMoneyOrderLogService {

    static Logger log = Logger.getLogger(UserTransferMoneyOrderLogService.class);

    public static UserTransferMoneyOrderLogService self() {
        return MultiDBTools.getService(UserTransferMoneyOrderLogService.class, true);
    }

    public static UserTransferMoneyOrderLogService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, UserTransferMoneyOrderLogService.class, true);
    }

    UserTransferMoneyOrderLogDao dao;

    public UserTransferMoneyOrderLogService(String jdbcKey) {
        dao = new UserTransferMoneyOrderLogDao(jdbcKey);
    }

    public UserTransferMoneyOrderLog getByTradeNo(String tradeNo){
        try {
            return dao.getById(tradeNo);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(String tradeNo,OrderLogContent.LogItem logItem){
        UserTransferMoneyOrderLog orderLog = getByTradeNo(tradeNo);
        if(orderLog != null && JsonTools.isJsonStr(orderLog.getContent())){
            OrderLogContent logContent = JsonTools.fromJson(orderLog.getContent(),OrderLogContent.class);
            logContent.getItems().add(logItem);

            UserTransferMoneyOrderLog updateEntity = new UserTransferMoneyOrderLog();
            updateEntity.setTradeNo(tradeNo);
            updateEntity.setContent(JsonTools.toJson(logContent));
            try {
                return dao.updateNotNull(updateEntity) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }else {
            OrderLogContent orderLogContent = new OrderLogContent();
            orderLogContent.getItems().add(logItem);

            orderLog = new UserTransferMoneyOrderLog();
            orderLog.setTradeNo(tradeNo);
            orderLog.setContent(JsonTools.toJson(orderLogContent));
            orderLog.setCreateDate(new Date());
            try {
                return dao.insert(orderLog) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean add(String tradeNo,OperateInfo operateInfo,String content, Integer state, String errorMsg){
        OrderLogContent.LogItem logItem = new OrderLogContent.LogItem();
        logItem.setCreateDate(new Date());
        logItem.setContent(content);
        logItem.setProgramType(operateInfo.getProgramType());
        logItem.setProgramTypeName(operateInfo.getProgramTypeName());
        logItem.setOperatorType(operateInfo.getOperatorType());
        logItem.setOperatorTypeName(operateInfo.getOperatorTypeName());
        logItem.setOperator(operateInfo.getOperator());

        logItem.setState(state);
        logItem.setErrorMsg(errorMsg);

        return add(tradeNo,logItem);
    }
}
